package com.apartment.sevice.impl;

import com.apartment.dao.AdminDao;
import com.apartment.data.entity.Admin;
import com.apartment.sevice.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Author : xuzhiyong
 * Date : 2022/5/30 11:39
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;

    @Override
    public Admin adminLogin(String username, String password) {
        return adminDao.findByUsernameAndPassword(username,password);
    }

    @Override
    public List<Admin> findAllAdmin() {
        return adminDao.findAll();
    }

    @Override
    public boolean addAdmin(Admin admin) {
        boolean flag = false;
        try {
            adminDao.save(admin);
            flag = true;
        }catch (Exception e){
            System.out.println("添加管理员失败！！！");
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean updateAdmin(Admin admin) {
        boolean flag = false;
        try {
            adminDao.save(admin);
            flag = true;
        }catch (Exception e){
            System.out.println("修改管理员失败！！！");
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean deleteAdminById(Integer id) {
        boolean flag = false;
        try {
            adminDao.deleteById(id);
            flag = true;
        }catch (Exception e){
            System.out.println("删除管理员失败！！！");
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public Admin findAdminById(Integer id) {
        try {
            Optional<Admin> admin = adminDao.findById(id);
            if (admin.isPresent()){
                return admin.get();
            }
        }catch (Exception e){
            System.out.println("查找失败");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Admin> findAdminByUsername(String username) {
        return adminDao.findAdminByUsername(username);
    }

    @Override
    public Admin findAdminByUsername1(String username) {
        return adminDao.findAdminByUsername1(username);
    }

    @Override
    public Admin findAdminByMail(String mail) {
        return adminDao.findAdminByMail(mail);
    }
}
